package com.ziJing.multithreading.sync;

import java.util.concurrent.Semaphore;

/**
 * @program Multithreading
 * @description:
 * @author: zhangbo
 * @create: 2022/05/03 15:52
 */
public class T10_TestSemaphore {

    static Semaphore s = new Semaphore(1);

    /**
     * 非公平的
     */
    static Semaphore semaphore = new Semaphore(2, true);

    public static void main(String[] args) {
        new Thread(() -> {
            try {
                s.acquire();
                Thread.sleep(100);
                System.out.println("Thread running.....");

                s.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();


        new Thread(() -> {
            try {
                s.acquire();
                Thread.sleep(100);
                System.out.println("Thread running.....");

                s.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();


        new Thread(() -> {
            try {
                s.acquire();
                Thread.sleep(100);
                System.out.println("Thread running.....");

                s.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();


        new Thread(() -> {
            try {
                s.acquire();
                Thread.sleep(100);
                System.out.println("Thread running.....");

                s.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

}
